package com.yb.web.controller.model;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yb.framework.core.domain.AjaxObject;
import com.yb.framework.core.domain.PageResult;
import com.yb.framework.core.utils.StringUtil;
import com.yb.framework.log.annotation.Log;
import com.yb.framework.log.enums.OperateTypeEnum;
import com.yb.module.model.domain.ModelThingParam;
import com.yb.web.controller.base.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/model/thing_param")
@Api(tags = "实物参数管理")
public class ModelThingParamController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(ModelThingParamController.class.getName());
	
	private static String MODEL_THING_PARAM = "model/thing_param/thing_param";
	private static String MODEL_THING_PARAM_ADD = "model/thing_param/thing_param_add";
	private static String MODEL_THING_PARAM_EDIT = "model/thing_param/thing_param_edit";
	
	@GetMapping()
	@PreAuthorize("hasAuthority('model:thing_param')")
	public String main() {
		return MODEL_THING_PARAM;
	}
	
	@GetMapping("/add_view")
	@PreAuthorize("hasAuthority('model:thing_param:add')")
	public String addView() {
		return MODEL_THING_PARAM_ADD;
	}
	
	@GetMapping("/edit_view/{id}")
	@PreAuthorize("hasAuthority('model:thing_param:edit')")
	public String editView(@PathVariable("id") Long id, ModelMap model) {
		model.put("modelThingParam", modelThingParamService.getById(id));
		return MODEL_THING_PARAM_EDIT;
	}
	
	/**
	 * 实物参数列表
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('model:thing_param:list')")
	@ApiOperation(value="分页查询", notes="分页查询实物参数信息")
	public @ResponseBody PageResult list(ModelThingParam modelThingParam) {
		return getPageInfo(modelThingParamService.getPageByEntity(modelThingParam));
	}
	
	/**
	 * 新增实物参数
	 */
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('model:thing_param:add')")
	@ApiOperation(value="新增实物参数", notes="新增实物参数信息")
	@Log(label = "新增实物参数", operateType = OperateTypeEnum.ADD)
	public @ResponseBody AjaxObject add(ModelThingParam modelThingParam) {
		return toAjax(modelThingParamService.insertParam(modelThingParam), "新增实物参数" + "[" + modelThingParam.getName() + "]");
	}
	
	/**
	 * 编辑实物参数
	 */
	@PostMapping("/edit")
	@PreAuthorize("hasAuthority('model:thing_param:edit')")
	@ApiOperation(value="编辑实物参数", notes="编辑实物参数信息")
	@Log(label = "编辑实物参数", operateType = OperateTypeEnum.EDIT)
	public @ResponseBody AjaxObject edit(ModelThingParam modelThingParam) {
		return toAjax(modelThingParamService.updateParam(modelThingParam), "编辑实物参数" + "[" + modelThingParam.getName() + "]");
	}
	
	/**
	 * 删除实物参数
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('model:thing_param:delete')")
	@ApiOperation(value="删除实物参数", notes="批量删除实物参数信息")
	@Log(label = "删除实物参数", operateType = OperateTypeEnum.DELETE)
	public @ResponseBody AjaxObject delete(@RequestParam("ids") String ids) {
		List<String> modelThingParamNames = modelThingParamService.getNamesByIds(StringUtil.str2LongArray(ids));
		return toAjax(modelThingParamService.deleteParam(StringUtil.str2LongArray(ids)), "批量删除实物参数" + modelThingParamNames);
	}
}